package com.qk.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.activity.ActivityDTO;
import com.qk.entity.Activity;
import com.qk.entity.Clue;
import com.qk.enums.FieldEnum;
import com.qk.enums.ParamEnum;
import com.qk.exception.CommonException;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import com.qk.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author SweetHurt
 * @description 针对表【activity(活动表)】的数据库操作Service实现
 * @createDate 2025-10-13 14:33:39
 */
@Service
@SuppressWarnings("all")
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity>
        implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ClueService clueService;

    @Override
    public List<Activity> getByType(Integer type) {
        if (ObjectUtil.isEmpty(type)) {
            CommonException.throwException(ParamEnum.PARAM_NOT_NULL_ERROR);
        }
        return list(Wrappers.<Activity>lambdaQuery().eq(Activity::getType, type).orderByDesc(Activity::getUpdateTime));
    }

    @Override
    public boolean deleteActivityById(Integer id) {
        List<Clue> list = clueService.list(Wrappers.<Clue>lambdaQuery().eq(Clue::getActivityId, id));
        if(!ObjectUtil.isEmpty(list)) {
            CommonException.throwException(FieldEnum.Field_NO_DELETE_USED_BY_CLUE);
        }
        return removeById(id);
    }

    @Override
    public boolean updateActivityById(Activity activity) {
        if (BeanUtil.hasNullField(activity, "discount", "voucher", "description", "updateTime")) {
            CommonException.throwException(ParamEnum.PARAM_NOT_NULL_ERROR);
        }
        activity.setUpdateTime(LocalDateTime.now());
        return updateById(activity);
    }

    @Override
    public boolean addActivity(Activity activity) {
        if (BeanUtil.hasNullField(activity, "id", "discount", "voucher", "updateTime", "createTime")) {
            CommonException.throwException(ParamEnum.PARAM_NOT_NULL_ERROR);
        }
        activity.setUpdateTime(LocalDateTime.now());
        activity.setCreateTime(LocalDateTime.now());
        return save(activity);
    }

    @Override
    public PageResult<Activity> getActivityByPage(ActivityDTO activityDTO) {
        // status == 1: 未开始 ; 开始时间 > now()
        // status == 2: 进行中 ; 开始时间 < now()  && 结束时间 > now()
        // status == 3: 已结束 ; 结束时间 < now()
        Page<Activity> list = page(
                new Page<>(activityDTO.getPage(), activityDTO.getPageSize()),
                Wrappers.<Activity>lambdaQuery()
                        .eq(ObjectUtil.isNotEmpty(activityDTO.getChannel()), Activity::getChannel, activityDTO.getChannel())
                        .eq(ObjectUtil.isNotEmpty(activityDTO.getType()), Activity::getType, activityDTO.getType())
                        .gt(ObjectUtil.isNotEmpty(activityDTO.getStatus()) && ObjectUtil.equals(activityDTO.getStatus(), 1), Activity::getStartTime, LocalDateTime.now())
                        .le(ObjectUtil.isNotEmpty(activityDTO.getStatus()) && ObjectUtil.equals(activityDTO.getStatus(), 2), Activity::getStartTime, LocalDateTime.now())
                        .ge(ObjectUtil.isNotEmpty(activityDTO.getStatus()) && ObjectUtil.equals(activityDTO.getStatus(), 2), Activity::getEndTime, LocalDateTime.now())
                        .gt(ObjectUtil.isNotEmpty(activityDTO.getStatus()) && ObjectUtil.equals(activityDTO.getStatus(), 3), Activity::getEndTime, LocalDateTime.now())
                        .orderByDesc(Activity::getUpdateTime)
        );
        return PageResult.<Activity>builder()
                .total(Long.valueOf(list.getTotal()).intValue())
                .rows(list.getRecords())
                .build();
    }
}




